home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0098_Trim Strings.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  6KB  |  117 lines

  1.  
  2. procedure White2Space( var Str: string; const WhiteSpace: string ); assembler;
  3.   { replace white space chars in Str by spaces
  4.     the string WhiteSpace contains the chars to replace }
  5. asm     { setup }
  6.         cld                      { string operations forwards    }
  7.         les   di, str            { ES:DI points to Str           }
  8.         xor   cx, cx             { clear cx                      }
  9.         mov   cl, [di]           { length Str in cl              }
  10.         jcxz  @exit              { if length of Str = 0, exit    }
  11.         inc   di                 { point to 1st char of Str      }
  12.         mov   dx, cx             { store length of Str           }
  13.         mov   bx, di             { pointer to Str                }
  14.         lds   si, WhiteSpace     { DS:SI points to WhiteSpace    }
  15.         mov   ah, [si]           { load length of WhiteSpace     }
  16.  
  17. @start: cmp   ah, 0              { more chars WhiteSpace left?   }
  18.         jz    @exit              { no, exit                      }
  19.         inc   si                 { point to next char WhiteSpace }
  20.         mov   al, [si]           { next char to hunt             }
  21.         dec   ah                 { ah counting down              }
  22.         xor   dh, dh             { clear dh                      }
  23.         mov   cx, dx             { restore length of Str         }
  24.         mov   di, bx             { restore pointer to Str        }
  25.         mov   dh, ' '            { space char                    }
  26. @scan:
  27.   repne scasb                    { the hunt is on                }
  28.         jnz   @next              { white space found?            }
  29.         mov   [di-1], dh         { yes, replace that one         }
  30. #next:  jcxz  @start             { if no more chars in Str       }
  31.         jmp   @scan              { if more chars in Str          }
  32. @exit:
  33. end  { White2Space };
  34.  
  35.  
  36. procedure Trim( var Str: string ); assembler;
  37.   { remove trailing and leading spaces from str }
  38. asm     { setup }
  39.         les   di, str            { ES:DI points to Str                }
  40.         lds   si, str            { DS:SI points to Str                }
  41.         xor   cx, cx             { clear cx                           }
  42.         mov   cl, [di]           { length Str in cl                   }
  43.         jcxz  @exit              { if length of Str = 0, exit         }
  44.         mov   bx, di             { bx points to length byte of Str    }
  45.         xor   dx, dx             { clear dx                           }
  46.         mov   al, ' '            { hunt for spaces                    }
  47.  
  48.         { look for trailing spaces }
  49.         std                      { string operations backwards        }
  50.         add   di, cx             { start with last char in Str        }
  51.    repe scasb                    { the hunt is on                     }
  52.         jz    @done              { only spaces?                       }
  53.         inc   cx                 { no, don't lose last char           }
  54.  
  55.         { look for leading spaces }
  56.         cld                      { string operations forward          }
  57.         inc   si                 { pointer to 1st char of Str         }
  58.         mov   di, si             { pointer to 1st char of Str --> di  }
  59.    repe scasb                    { the hunt is on                     }
  60.         jz    @done              { if only spaces, we are done        }
  61.         inc   cx                 { no, don't lose 1st non-blank char  }
  62.         dec   di                 { no, don't lose 1st non-blank char  }
  63.         mov   dx, cx             { new lenght of Str                  }
  64.         xchg  di, si             { swap si and di                     }
  65.     rep movsb                    { move remaining part of Str         }
  66. @done:  mov   [bx], dl           { new length of Str                  }
  67. @exit:
  68. end  { Trim };
  69.  
  70. procedure RTrim( var Str: string ); assembler;
  71.   { remove trailing spaces from str }
  72. asm     { setup }
  73.         std                      { string operations backwards   }
  74.         les   di, str            { ES:DI points to Str           }
  75.         xor   cx, cx             { clear cx                      }
  76.         mov   cl, [di]           { length Str in cl              }
  77.         jcxz  @exit              { if length of Str = 0, exit    }
  78.         mov   bx, di             { bx points to Str              }
  79.         add   di, cx             { start with last char in Str   }
  80.         mov   al, ' '            { hunt for spaces               }
  81.  
  82.         { remove trailing spaces }
  83.    repe scasb                    { the hunt is on                }
  84.         jz    @done              { only spaces?                  }
  85.         inc   cx                 { no, don't lose last char      }
  86. @done:  mov   [bx], cl           { overwrite length byte of Str  }
  87. @exit:
  88. end  { RTrim };
  89.  
  90.  
  91. procedure LTrim( var Str: string ); assembler;
  92.   { remove leading white space from str }
  93. asm     { setup }
  94.         cld                      { string operations forward          }
  95.         lds   si, str            { DS:SI points to Str                }
  96.         xor   cx, cx             { clear cx                           }
  97.         mov   cl, [si]           { length Str --> cl                  }
  98.         jcxz  @exit              { if length Str = 0, exit            }
  99.         mov   bx, si             { save pointer to length byte of Str }
  100.         inc   si                 { 1st char of Str                    }
  101.         mov   di, si             { pointer to 1st char of Str --> di  }
  102.         mov   al, ' '            { hunt for spaces                    }
  103.         xor   dx, dx             { clear dx                           }
  104.  
  105.         { look for leading spaces }
  106.    repe scasb                    { the hunt is on                     }
  107.         jz    @done              { if only spaces, we are done        }
  108.         inc   cx                 { no, don't lose 1st non-blank char  }
  109.         dec   di                 { no, don't lose 1st non-blank char  }
  110.         mov   dx, cx             { new lenght of Str                  }
  111.         xchg  di, si             { swap si and di                     }
  112.     rep movsb                    { move remaining part of Str         }
  113. @done:  mov   [bx], dl           { new length of Str                  }
  114. @exit:
  115. end  { LTrim };
  116.  
  117.